package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.entity.UserSellerBaseEntity;
import com.yuanfeng.userms.entity.UserSellerGroupEntity;
import com.yuanfeng.userms.mapper.UserSellerBaseMapper;
import com.yuanfeng.userms.mapper.UserSellerGroupMapper;
import com.yuanfeng.userms.service.UserSellerGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserSellerGroupServiceImpl extends ServiceImpl<UserSellerGroupMapper, UserSellerGroupEntity>
        implements UserSellerGroupService {

    @Resource
    private UserSellerBaseMapper userSellerBaseMapper;

    @Override
    public ResponseResult get(Map<String, String> paraMap) {
        String token = paraMap.get("token");
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(token);
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, token);
        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        QueryWrapper<UserSellerGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", userInfo.getShopId());
        List<UserSellerGroupEntity> entityList = this.baseMapper.selectList(queryWrapper);
        if (null == entityList) {
            return ResponseResult.fail(2, "没有权限组信息");
        }
        return ResponseResult.success(entityList);
    }

    @Override
    public ResponseResult add(Map<String, String> paraMap) {
        String token = paraMap.get("token");
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(token);
        // 权限判断
        if (userInfo == null) {
            return ResponseResult.fail(10086, "没有权限");
        }
        QueryWrapper<UserSellerGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", userInfo.getShopId())
                .eq("group_name", paraMap.get("groupName"));
        UserSellerGroupEntity entity = this.baseMapper.selectOne(queryWrapper);
        if (entity != null && null != entity.getGroupId()) {
            return ResponseResult.fail(2, "权限组已存在");
        }
        UserSellerGroupEntity userSellerGroupEntity = new UserSellerGroupEntity();
        userSellerGroupEntity.setGroupName(paraMap.get("groupName"));
        userSellerGroupEntity.setLimits(paraMap.get("limits"));
        userSellerGroupEntity.setShopId(Integer.valueOf(userInfo.getShopId()));
        userSellerGroupEntity.setSmtLimits("1");
        Integer torfInteger = this.baseMapper.insert(userSellerGroupEntity);
        ;
        if (1 == torfInteger) {
            return ResponseResult.success(1, "添加权限成功");
        }
        return ResponseResult.fail(2, "添加权限失败");
    }

    @Override
    public ResponseResult delete(Map<String, String> paraMap) {
        String token = paraMap.get("token");
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(token);
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, token);
        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        String groupId = paraMap.get("groupId");
        QueryWrapper<UserSellerBaseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seller_group_id", groupId).ne("seller_is_admin", 1);
        Integer count = userSellerBaseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(2, "删除权限组信息失败，该权限组下还有用户！");
        }
        Integer torfInteger = this.baseMapper.deleteById(groupId);
        if (1 == torfInteger) {
            return ResponseResult.success(1, "删除成功");
        }
        return ResponseResult.fail(2, "删除失败");
    }

    @Override
    public ResponseResult update(Map<String, String> paraMap) {
        String token = paraMap.get("token");
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(token);
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, token);
        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        paraMap.put("shopId", userInfo.getShopId());
        UserSellerGroupEntity userSellerGroupEntity = new UserSellerGroupEntity();
        userSellerGroupEntity.setGroupId(Integer.valueOf(paraMap.get("groupId")));
        userSellerGroupEntity.setGroupName(paraMap.get("groupName"));
        userSellerGroupEntity.setLimits(paraMap.get("limits"));
        Integer torfInteger = this.baseMapper.updateById(userSellerGroupEntity);
        if (1 == torfInteger) {
            return ResponseResult.success(1, "修改权限成功");
        }
        return ResponseResult.fail(2, "修改权限失败");
    }
}




